import { _decorator, Node, tween, Tween, v3 } from "cc";
import { Ab_BaseUI_bA } from "./ui.base";

const {ccclass, property} = _decorator;

@ccclass
export abstract class Ab_BasePopUp_bA<Ab_POPUP_bA, Ab_RETURN_bA> extends Ab_BaseUI_bA {
    /** 弹窗是否被关闭 */
    public get Ab_is_close_bA(){
        return this.Ab_npromise_bA==undefined;
    }
	private Ab_npromise_bA: abi.Ab_NudityPromise_bA<Ab_RETURN_bA>;
    private Ab_closed_bA: boolean;
    /** 生成一个promise实例 */
    protected Ab_promise_bA(){
        this.Ab_closed_bA = false;
		this.Ab_npromise_bA = new abi.Ab_NudityPromise_bA();
        return this.Ab_npromise_bA.Ab_promise_bA;
    }
    /**
     * 关闭弹窗
     * @param param 回复数据，如果不传，则会被当作弹窗操作失败传入默认参数
     */
    protected Ab_close_bA(param?: Ab_RETURN_bA){
        if(this.Ab_closed_bA) return undefined;
        else this.Ab_closed_bA = true;
        // 调用回调函数
        this.Ab_onClose_bA(param);
        // 回复promise
        if(param!==undefined) this.Ab_npromise_bA.Ab_resolve_bA(param);
        else this.Ab_npromise_bA.Ab_reject_bA(this.Ab_default_return_bA);
        // 清空promise相关的响应
		this.Ab_npromise_bA = undefined;
    }

    /** 从外部关闭弹窗 */
    public Ab_closeFromOutside_bA(){
        this.Ab_close_bA(this.Ab_default_return_bA);
    }

    /** 默认返回值 */
    protected abstract Ab_default_return_bA: Ab_RETURN_bA;
	/** 管理节点 */
	protected Ab_pop_bA: Ab_POPUP_bA;
	/**
	 * 显示弹窗并返回promise
	 * @param args
	 */
	public Ab_show_bA(pop: Ab_POPUP_bA, ...args: Parameters<typeof this.Ab_onShow_bA>){
		this.Ab_pop_bA = pop;
		this.node.active = true;
		this.Ab_call_func_bA(this.Ab_onShow_bA, ...args);
		return this.Ab_promise_bA();
	}
    /**
     * 显示时调用
     * @param param 可选参数
     */
    public abstract Ab_onShow_bA(...param: any[]): void;
    /** 关闭弹窗时的回调 */
    protected Ab_onClose_bA(param: Ab_RETURN_bA): void{};

	/**
	 * 果冻效果（入场）
	 * @param node
	 */
	protected static Ab_jelly_enter_bA(node: Node, duration?: number){
		let np = new abi.Ab_NudityPromise_bA<Node>();
		Tween.stopAllByTarget(node);
		duration = duration ?? 0.5;

		tween(node)
		.set({scale: v3(0.8, 0.8, 1)})
		.to(duration, {scale: v3(1, 1, 1)}, {easing: 'elasticOut'})
		.set({scale: v3(1, 1, 1)})
		.call(()=>np.Ab_resolve_bA(node))
		.start();

		return np.Ab_promise_bA;
	}

	/**
	 * 果冻效果（离场）
	 * @param node
	 * @param duration
	 */
	protected static Ab_jelly_leave_bA(node: Node, duration?: number){
		let np = new abi.Ab_NudityPromise_bA<Node>();
		Tween.stopAllByTarget(node);
		duration = duration ?? 0.25;

		tween(node)
		.to(duration, {scale: v3(0.8, 0.8, 1)}, {easing: "backIn"})
		.set({scale: v3(0.8, 0.8, 1)})
		.call(()=>np.Ab_resolve_bA(node))
		.start();

		return np.Ab_promise_bA;
	}
}
